package com.test.memoryapp;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

/**
 * This Activity is for manager of vocabs.<BR>
 * It can display, register and delete the vocabs.<BR>
 * This activity can import some files for register some vocabs conveniently.
 * <P>
 * 이 액티비티는 어휘를 관리합니다.<BR>
 * 어휘를 표시, 등록, 삭제 할 수 있습니다.<BR>
 * 또한 편리한 어휘등록을 위해 어휘파일을 가져올 수 있습니다.
 * @author sppurple
 *
 */
public class VocaMngActivity extends ListActivity{
	static final int MENU_ADD_VOCAB = 1;
	static final int MENU_IMPORT_FILE = 2;
	static final int MENU_DELETE_ALL = 3;
	private VocabsDbAdapter mDbHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.voca_mng);
		mDbHelper = new VocabsDbAdapter(this);
		mDbHelper.open();
		fillData();
	}
	
	
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ADD_VOCAB, 0, "Add Vocab");
        menu.add(0, MENU_IMPORT_FILE, 0, "Import File");
        menu.add(0, MENU_DELETE_ALL, 0, "Delete All");
        return result;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
	        case MENU_ADD_VOCAB:
	            addVocab();
	            return true;
	        case MENU_IMPORT_FILE:
	        	String FilePath = "/mnt/sdcard/vocabs.txt";
				
				SSHFileReader fileReader = new SSHFileReader(FilePath, "", "/", new String[]{"words","parts","meaning"});
				
				try {
					List<Map<String,String>> vocabList = fileReader.getList();
					addVocabList(vocabList);
				} catch (IOException e) {
					e.printStackTrace();
				}
	        	return true;
	        	
	        case MENU_DELETE_ALL :
	        	deleteDb();
	        	return true;
        }
       
        return super.onOptionsItemSelected(item);
    }

	private void addVocab() {		
        mDbHelper.createNote("Words", "Parts", "Meaning");
        fillData();
    }
	
	private void deleteDb() {
		mDbHelper.deleteAll();
		fillData();
	}
	
	private void addVocabList(List<Map<String,String>> vocabList) {
		for (Map<String,String> vocab:vocabList){
	        mDbHelper.createNote(vocab.get("words"),vocab.get("parts"),vocab.get("meaning") );
		}		
        fillData();
    }
	
	private void fillData() {
        Cursor c = mDbHelper.fetchAllVocabs();
        startManagingCursor(c);

        String[] from = new String[] { VocabsDbAdapter.KEY_WORDS, VocabsDbAdapter.KEY_PARTS, VocabsDbAdapter.KEY_MEANING, VocabsDbAdapter.KEY_TRG_FLAG, VocabsDbAdapter.KEY_POINT_EXPOSURE};
        int[] to = new int[] { R.id.words, R.id.parts, R.id.meaning, R.id.trg_flag, R.id.point_exposure };

        VocabAdapter vocabs =
            new VocabAdapter(this, R.layout.vocab_row, c, from, to);
        
        setListAdapter(vocabs);
    }



	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		CheckBox cb = (CheckBox) ((LinearLayout)((LinearLayout)v).getChildAt(0)).getChildAt(0);
		
		if (cb.isChecked()) cb.setChecked(false);
		else cb.setChecked(true);
	}
}
